package scau.zhong.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import scau.zhong.common.page.PageResult;
import scau.zhong.mapper.PersonMapper;
import scau.zhong.pojo.Person;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class PersonService  implements scau.zhong.service.PersonService {

    @Autowired
    private PersonMapper personMapper;

    @Override
    public List<Person> getAll() {
        return personMapper.selectAll();
    }

    @Override
    public Person findById(String id) {
        return personMapper.selectByPrimaryKey(id);
    }

    @Override
    public void insert(Person person) {
        personMapper.insert(person);
    }

    @Override
    public void update(Person person) {
        personMapper.updateByPrimaryKeySelective(person);
    }

    @Override
    public int deleteById(String id) {
        return personMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageResult<Person> queryByPage(String key, Integer page, Integer rows) {
        Example example = new Example(Person.class);
        Example.Criteria criteria = example.createCriteria();
        //根据name模糊查询
        if(StringUtils.isNotBlank(key)){
            criteria.andLike("name","%"+key+"%");
        }
        //添加分页条件
        PageHelper.startPage(page,rows);
        //添加排序条件
//        if(StringUtils.isNotBlank(sortBy)){
//            example.setOrderByClause(sortBy + " " + (desc?"desc":"asc"));
//        }
        List<Person> personList = this.personMapper.selectByExample(example);
        PageInfo<Person> pageInfo = new PageInfo<>(personList);
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getPages(),pageInfo.getList());
    }


}
